//
//  AddOrgViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/28.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AddOrgViewController.h"
#import "ResizeView.h"
#import "PlacePickerView.h"
#import "WCPickerView.h"
#import "HttpOrgAction.h"

@interface AddOrgViewController (){
    int status;
}
@property (nonatomic) OrgType orgType;
@property (nonatomic,strong) UITextField *nameTF;
@property (nonatomic,strong) UITextField *codeTF;
@property (nonatomic,strong) NSString *province;
@property (nonatomic,strong) NSString *city;
@property (nonatomic,strong) NSString *area;
@property (nonatomic,strong) UITextField *addressTF;
@property (nonatomic,strong) UITextField *personTF;
@property (nonatomic,strong) UITextField *phoneTF;
@property (nonatomic,strong) AddOrgBlock addOrgBlock;
@end



@implementation AddOrgViewController

-(id)initWithOrgType:(OrgType)orgType withBlock:(AddOrgBlock)block{
    if (self == [super init]) {
        self.orgType = orgType;
        self.addOrgBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"添加机构";
    
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:10]];
    ResizeViewInfo *nameRV = [self getNormalResizeViewInfo:@"机构名称:"];
    self.nameTF = (UITextField*)[nameRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:nameRV];
    ResizeViewInfo *codeRV = [self getNormalResizeViewInfo:@"机构代码:"];
    self.codeTF = (UITextField*)[codeRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:codeRV];
    NSString *orgString;
    if (self.orgType == OrgTypeAgent) {
        orgString = @"代理商";
    }else if (self.orgType == OrgTypeChannel){
        orgString = @"渠道商";
    }else if (self.orgType == OrgTypeAdvertiser){
        orgString = @"广告商";

    }
    [resizeView addResizeSubview:[self getTitleResizeViewInfo:@"机构类型:" value:orgString]];
    [resizeView addResizeSubview:[self getOrgStatusResizeViewInfo:@"机构状态:" value:@""]];
    [resizeView addResizeSubview:[self getProvinceResizeViewInfo:@"省市区:" value:@""]];
    ResizeViewInfo *addressRV = [self getNormalResizeViewInfo:@"详细地址:"];
    self.addressTF = (UITextField*)[addressRV viewWithKey:@"TF"];

    [resizeView addResizeSubview:addressRV];
    
    ResizeViewInfo *personRV = [self getNormalResizeViewInfo:@"联系人:"];
    self.personTF = (UITextField*)[personRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:personRV];
    
    ResizeViewInfo *phoneRV = [self getNormalResizeViewInfo:@"联系电话:"];
    self.phoneTF = (UITextField*)[phoneRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:phoneRV];

    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:50]];
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];

}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)title{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UITextField *nameTF = [UITextField new];
    nameTF.textColor = [UIColor grayColor];
    nameTF.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:nameTF viewKey:@"TF"];
    nameTF.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 10);
    return resizeViewInfo;
}

-(ResizeViewInfo*)getTitleResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.text = valueString;
    [resizeViewInfo addSubview:nameLabel];
    nameLabel.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(150);
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getProvinceResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *nameButton = [UIButton new];
    [nameButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    nameButton.titleLabel.font = [UIFont systemFontOfSize:16];
    nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [resizeViewInfo addSubview:nameButton];
    nameButton.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(200);
    [nameButton addTarget:self action:@selector(nameAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getOrgStatusResizeViewInfo:(NSString*)title value:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 44;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    [resizeViewInfo addSubview:titleLabel];
    titleLabel.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).autoWidthRatio(0);
    [titleLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    UIButton *nameButton = [UIButton new];
    [nameButton setTitle:@"不经营" forState:UIControlStateNormal];
    [nameButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    nameButton.titleLabel.font = [UIFont systemFontOfSize:16];
    nameButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [resizeViewInfo addSubview:nameButton];
    nameButton.sd_layout.leftSpaceToView(titleLabel, 10).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(200);
    [nameButton addTarget:self action:@selector(statusAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return resizeViewInfo;
}

-(void)statusAction:(id)sender{
    UIButton *button = sender;
    [self.view endEditing:YES];
    NSArray *array = @[@"不经营",@"经营中",@"过期"];
    WCPickerView *wcPickerView = [[WCPickerView alloc] initViewWithArray:array];
    [wcPickerView setWcPickSuccessWithBlock:^(NSInteger result) {
        status = (int)result;
        [button setTitle:array[result] forState:UIControlStateNormal];
    }];
    [self.view addSubview:wcPickerView];
}

-(void)nameAction:(id)sender{
    [self.view endEditing:YES];
    UIButton *button = sender;
    PlacePickerView *placePickerView = [[PlacePickerView alloc] initViewWithArray:^(NSString *province, NSString *city, NSString *area) {
        self.province = province;
        self.city = city;
        self.area = area;
        [button setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,area] forState:UIControlStateNormal];
    }];
    [self.view addSubview:placePickerView];
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    
    return resizeViewInfo;
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 40;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if ([self.nameTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入名称" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.codeTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入代码" autoDissmissAfterDelay:0];
        return;
    }
    if (self.city.length == 0) {
        [self showMessage:@"请输入省市区" autoDissmissAfterDelay:0];
        return;
    }
    
    if ([self.personTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入联系人" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.phoneTF.text isEqualToString:@""]) {
        [self showMessage:@"请输入联系电话" autoDissmissAfterDelay:0];
        return;
    }
    
    OrgInfo *orgInfo = [[OrgInfo alloc] init];
    orgInfo.name = self.nameTF.text;
    orgInfo.orgCode = self.codeTF.text;
    orgInfo.orgType = [NSString stringWithFormat:@"%d",self.orgType];
    orgInfo.city = self.city;
    orgInfo.area = self.area;
    orgInfo.province = self.province;
    orgInfo.address = self.addressTF.text;
    orgInfo.status = [NSString stringWithFormat:@"%d",status];;
    orgInfo.createTime = [Utils dateToString:[NSDate date]];
    orgInfo.contractor = self.personTF.text;
    orgInfo.contractPhone = self.phoneTF.text;
    orgInfo.province = self.province;
    
    [HttpOrgAction postAddOrgInfo:orgInfo block:^(id result, NSError *error) {
        if (!error) {
            [self showMessage:@"添加成功" autoDissmissAfterDelay:0];
            if (self.addOrgBlock) {
                self.addOrgBlock();
            }
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
            } afterDelay:0.8];
        }else{
            [self showMessage:error.domain autoDissmissAfterDelay:0];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
